package com.cloud.api.domain.report;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * @author 杨斌
 * 2025-01-2025-01-17
 */
@Data
@TableName("t_vehicle_aggregation")
@Schema(name = "VehicleAggregation", description = "车辆聚集停放")
public class VehicleAggregation implements Serializable {

    private static final long serialVersionUID = 1L;


    @Schema(description = "vin")
    @TableField("vin")
    private String vin;

    @Schema(description = "簇id")
    @TableField("cluster_id")
    private Integer clusterId;

    @Schema(description = "纬度")
    @TableField("lat")
    private Double lat;

    @Schema(description = "经度")
    @TableField("lng")
    private Double lng;

    @Schema(description = "上报时间")
    @TableField("op_time")
    private LocalDateTime opTime;

    @Schema(description = "soc")
    @TableField(value = "soc")
    private Short soc;


    @Schema(description = "车辆位置")
    @TableField("address")
    private String address;
    @Schema(description = "省")
    @TableField("province")
    @JsonIgnore
    private String province;
    @Schema(description = "市")
    @TableField("city")
    @JsonIgnore
    private String city;
    @Schema(description = "区/县")
    @TableField("district")
    @JsonIgnore
    private String district;

    @Schema(description = "所属部门")
    @TableField("dept")
    private String dept;

    @TableField(exist = false)
    @JsonIgnore
    private boolean visited = false;

    @TableField(exist = false)
    @JsonIgnore
    private boolean noise = false;

    // 计算两点之间的距离（单位：米）
    public double distance(VehicleAggregation other) {
        double radiusOfEarth = 6371.0;
        double lat1 = Math.toRadians(this.lat / 1000000.0);
        double lat2 = Math.toRadians(other.lat / 1000000.0);
        double lng1 = Math.toRadians(this.lng / 1000000.0);
        double lng2 = Math.toRadians(other.lng / 1000000.0);

        double latDistance = lat2 - lat1;
        double lngDistance = lng2 - lng1;

        double a = Math.pow(Math.sin(latDistance / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngDistance / 2), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        double distance = radiusOfEarth * c *1000.0;
        // 保留两位小数
        BigDecimal bd = new BigDecimal(distance);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
}
